﻿/**
 * CircleEmitter
 *
 * @author		Allen Chou
 * @version 	1.1.1 (last update: Aug 26 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

/*
 * Copyright 2008 (C) Allen Chou (cjcat2266@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package idv.cjcat.display.particles {
	import flash.display.*;
	import flash.events.*;
	
	import idv.cjcat.*;
	import idv.cjcat.display.*;
	import idv.cjcat.geom.*;
	
	public class CircleEmitter extends AbstractEmitter {
		
		public  var even:Boolean = false;
		private var _radius:Number = 50;
		
		public function CircleEmitter(displayClass:Class = null, container:DisplayObjectContainer = null) {
			super(displayClass, container);
		}
		
		override public function spawn():DisplayObject {
			var temp:DisplayObject = new displayClass();
			var randLength:Number = _radius * ((even)?(Math.sqrt(Math.random())):(Math.random()));
			var randAng:Number = Math.random() * Math.PI * 2;
			temp.x = centerX + randLength * Math.cos(randAng);
			temp.y = centerY + randLength * Math.sin(randAng);
			return temp;
		}
		
		private var a:int;
		private var b:int;
		private var tempDir:Vector2;
		private var dev:Number;
		private var posAng:Number;
		private var arr:Array;
		public function uniBurst(count:int = 10, repetitions:int = 1, interval:Number = 1, directionDeviation:Number = 0, initPhase:Number = 0, phaseShift:Number = 0):void {
			if (count <= 0) return;
			if (repetitions <= 0) return;
			if (interval <= 0) return;
			
			for (b = 1; b <= repetitions; b++) {
				tempDir = _direction.unitVec().rotate(directionVar + 90, false);
				arr = new Array();
				
				for (a = 1; a <= count; a++) {
					dev = directionDeviation / 180 * Math.PI;
					posAng = Math.PI * 2 / count * (a - 1) + (b - 1) * phaseShift + initPhase;
					arr.push( { x:centerX + _radius * Math.cos(posAng), y:centerY + _radius * Math.sin(posAng), vx:speed * Math.cos(posAng + dev), vy:speed * Math.sin(posAng + dev) } );
					tempDir = tempDir.rotate(360 / count, false);
				}
				
				_uniBurstQueue.push({life: interval * (b - 1), array:arr});
			}
			
		}
		
		override public function getConfig(mask:EmitterConfigMask = null):Object {
			if (mask == null) mask = new EmitterConfigMask();
			var obj:Object = super.getConfig(mask);
			obj.radius = this.radius;
			obj.even = this.even;
			
			return obj;
		}
		
		//accessors
		//------------------------------------------------------------------------------------------------
		
		override public function set showAuxShape(value:Boolean):void {
			if (_auxShape != null) removeChild(_auxShape);
			_showAuxShape = value;
			if (value) {
				_auxShape = new Shape();
				CJDisplay.setLineStyle(_auxShape, auxShapeLineStyle);
				_auxShape.graphics.drawCircle(0, 0, _radius);
				updateAuxShape();
				addChild(_auxShape);
			} else {
				if (_auxShape != null) removeChild(_auxShape);
				_auxShape = null;
			}
		}
		
		override public function updateAuxShape(e:Event = null):void {
			if (_auxShape != null) {
				_auxShape.x = centerX;
				_auxShape.y = centerY;
			}
		}
		
		public function get radius():Number { return _radius; }
		public function set radius(value:Number):void {
			_radius = value;
			updateAuxShape();
		}
		
		//end of accessors
		//------------------------------------------------------------------------------------------------
		
	}
}